#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
const int N=10005;
const int M=100050;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
struct Edge{
    int to,cap,flow,next;
    double cost;
}edge[M];
int head[N],cnt;
int pre[N];
double d[N];
bool vis[N];
int T,n,m;
int blo[N][2];
int s,t;
int u,v,c;
double p;
int nn;
void init(int n){
    nn=n;
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addEdge(int u,int v,int cap,double cost){
    edge[cnt].to=v;
    edge[cnt].cap=cap;
    edge[cnt].cost=cost;
    edge[cnt].flow=0;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].to=u;
    edge[cnt].cap=0;
    edge[cnt].cost=-cost;
    edge[cnt].flow=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
bool spfa(int s,int t){
    queue<int> q;
    for(int i=0;i<=nn;i++){
        d[i]=INF;
        vis[i]=false;
        pre[i]=-1;
    }
    d[s]=0;
    vis[s]=true;
    q.push(s);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(edge[i].cap >edge[i].flow && d[v]>d[u]+edge[i].cost+eps){
                d[v]=d[u]+edge[i].cost;
                pre[v]=i;
                if(!vis[v]){
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t]==-1){
        return false;
    }
    else{
        return true;
    }
}
int maxFLow;
double minCost;
void mcmf(int s,int t){
    maxFLow=0;
    minCost=0;
    while(spfa(s,t)){
        int Min=INF;
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){
            if(Min>edge[i].cap-edge[i].flow){
                Min=edge[i].cap-edge[i].flow;
            }
        }
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){
            edge[i].flow+=Min;
            edge[i^1].flow-=Min;
            minCost+=edge[i].cost*Min;
        }
        maxFLow+=Min;
    }
}
int main(void){
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        init(n+50);
        s=n+1,t=n+2;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&blo[i][0],&blo[i][1]);
            int t=min(blo[i][0],blo[i][1]);
            blo[i][0]-=t;
            blo[i][1]-=t;
        }
        for(int i=0;i<m;i++){
            scanf("%d%d%d%lf",&u,&v,&c,&p);
            p=-log10(1.0-p);
            //拆边
            if(c>0){
                addEdge(u,v,1,0.0);
            }
            if(c>1){
                addEdge(u,v,c-1,p);
            }
        }
        for(int i=1;i<=n;i++){
            if(blo[i][0]>0){
                addEdge(s,i,blo[i][0],0);
            }
            else if(blo[i][1]>0){
                addEdge(i,t,blo[i][1],0);
            }
        }
        mcmf(s,t);
        double ans=1.0-pow(10,-minCost);
        printf("%.2lf\n",ans);
    }
    return 0;
}